package com.rc.web.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.paginate.Page;
import com.rc.common.annotate.WithoutTenant;
import com.rc.pojo.dto.user.AddUserDTO;
import com.rc.pojo.dto.user.PageUserDTO;
import com.rc.pojo.dto.user.RestPasswordDTO;
import com.rc.pojo.dto.user.UserLoginDTO;
import com.rc.pojo.po.User;
import com.rc.pojo.vo.user.UserLoginVO;
import com.rc.pojo.vo.user.UserPageVO;
import com.rc.pojo.vo.user.UserVO;
import com.rc.web.converter.UserConverter;
import com.rc.web.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 员工信息 控制层。
 *
 * @author sym
 * @since 2024-05-24
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    private final UserConverter userConverter;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @WithoutTenant
    public UserLoginVO login(@Validated @RequestBody UserLoginDTO dto){
       return userService.login(dto);
    }

    @PostMapping
    @ApiOperation("农场管理员添加用户")
    public void add(@RequestBody AddUserDTO dto){
        userService.add(dto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("农场管理员删除用户")
    public void deleteById(@PathVariable Long id){
        userService.deleteById(id);
    }

    @GetMapping("/page")
    @ApiOperation("农场管理员分页查看用户")
    public Page<UserPageVO> getPage(PageUserDTO dto) {
        return userService.getPage(dto);
    }

    @GetMapping("/{id}")
    @ApiOperation("查看员工详情")
    public UserVO getOneById(@PathVariable Long id){
        return userService.getOneById(id);
    }

    @GetMapping("/list")
    public List<UserVO> getList(){
        return userService.getList();
    }

    @PutMapping("/restPassword")
    @ApiOperation("修改密码")
    public void RestPassword(@RequestBody RestPasswordDTO dto){
        userService.restPassword(dto);
    }

    @PutMapping
    @ApiOperation("更新员工信息")
    public void RestPassword6(@RequestBody Map<String, Object> dto){
        userService.restUser(dto);
    }

    @GetMapping("/info")
    public UserLoginVO info(){
        User user = StpUtil.getSession().getModel("user", User.class);
        UserLoginVO loginVO = userConverter.toLoginVO(user);
        loginVO.setToken(StpUtil.getTokenValue());
        return loginVO;
    }
}
